// Include libraries
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "user_interface.h"
#include "sio.h"
#include "LCD.h"

void set_data_directions(void)
{
  	// Enable LEDs and turn them on
  	DDRC |= _BV(PC5) | _BV(PC6);
  	PORTC &= ~_BV(PC6) & ~_BV(PC5);

	// Backlight
	DDRB |= _BV(PB0) | _BV(PB1) | _BV(PB2) | _BV(PB3);

	DDRC |= _BV(PC0) | _BV(PC1) | _BV(PC2) | _BV(PC3) | _BV(PC4);

	DDRD |= _BV(PD5) | _BV(PD6) | _BV(PD7);

	PORTB |= _BV(PB0);
}


// Following three functions taken from printf sample program by Andrew Sterian
/*
 * This function is the ultimate output function of printf(). It takes a
 * character to print and a file stream. We can ignore the file stream if
 * there is only place for characters to go. This function simply calls
 * the "output()" function in the SIO module.
 */
static int stdio_put(char c, FILE *stream)
{
  (void)stream;  // Tell C compiler that "stream" is unused so we
                 // don't get any warnings.

  output(c);

  return 0;      // 0 return value indicates success
}

/*
 * This function is the ultimate input function of scanf(). It takes a
 * file stream to read from, which we ignore since we will always read
 * characters from the SIO module. The function simply calls the "inchar()"
 * function in the SIO module and returns the character read.
 */
static int stdio_get(FILE *stream)
{
  (void)stream;

  return inchar();
}

/*
 * The variable "mystream" serves as the Standard I/O FILE structure for both
 * reading and writing. It will be used for stdio, stdout, and stderr. Note
 * that it is passed the address of the "stdio_put()" and "stdio_get()" functions
 * defined above.
 */
static FILE mystream = FDEV_SETUP_STREAM(stdio_put, stdio_get, _FDEV_SETUP_RW);


int main(void)
{
	// Set all Port I/O pins and directions
	set_data_directions();

	// Initialize the serial port communications
	sio_init();

	// Enable interrupts
	sei();

	//Set the Standard I/O file streams to the address of our FILE structure
	stdin = stdout = stderr = &mystream;

	sei();

	LCD_Init();

	while(1)
	{
		get_Input();
	}

	return 0;
}
